<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,initial-scale=1.0,width=device-width" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>二维码解析</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <style type="text/css">
        html,
        body {
            height: 100%;
            margin: 0;
            background-color: #fff;
        }

        .div_cancel {
            width: 18px;
            height: 18px;
            float: right;
            background-image: url(../image/fork.png);
            background-position: center;
            background-repeat: no-repeat;
            background-size: 18px 18px;
            margin: 35px;
        }

        .div_icon {
            height: 76px;
            background-image: url(../image/authorize.png);
            background-position: center;
            background-repeat: no-repeat;
            background-size: 85px 76px;
        }

        .div_name {
            color: #101010;
            font-size: 16px;
            margin-top: 20px;
            margin-left: 30px;
            margin-right: 30px;
            line-height: 25px;
        }

        .div_style {
            text-align: center;
            color: #8f8f8f;
            font-size: 14px;
            margin-top: 10px;
        }

        .div_ip {
            color: #8f8f8f;
            font-size: 14px;
            text-align: center;
            margin-top: 20px;
        }

        .div_determine {
            width: 130px;
            height: 40px;
            background-color: #ff8000;
            line-height: 40px;
            text-align: center;
            border-radius: 5px;
            font-size: 17px;
            margin-top: 80px;
            color: #fff;
            display: none;
        }

        .div_promptboc {
            height: 160px;
            width: 80%;
            background-color: #fff;
            position: absolute;
            top: 0;
            right: 10%;
            border-radius: 5px;
        }

        .div_promptboc_name {
            text-align: center;
            font-size: 14px;
            margin-top: 15px;
            color: #101010;
        }

        .div_qrcode_input {
            width: 100px;
            height: 20px;
            border: 0px;
            outline: none;
            margin-top: 25px;
            text-align: center;
        }

        .div_qrcode_line {
            width: 100%;
            height: 1px;
            line-height: 1px;
            font-size: 10px;
            color: #cbcaca
        }

        .div_promptboc_determine {
            height: 45px;
            width: 100%;
            line-height: 45px;
            text-align: center;
            position: absolute;
            bottom: 0;
            font-size: 18px;
            background-color: #fea33a;
            color: #fff;
            border-bottom-right-radius: 5px;
            border-bottom-left-radius: 5px;
        }
    </style>
</head>

<body>

    <div class="div_cancel">
    </div>
    <div style="height:150px">
    </div>
    <div class="div_icon">
    </div>
    <div class="div_name">

    </div>
    <div class="div_ip">

    </div>
    <div class="div_mac div_style">

    </div>

    <div class="div_determine"> 确定</div>
    <div class="div_promptboc" style="display:none">
        <div class="div_promptboc_name">请为该设备添加备注
            <div>
                <div style="font-size:15px">
                    <input type="text" name="firstname" class="div_qrcode_input" placeholder="" style="font-size:17px;color:#101010" />
                </div>
                <div class="div_qrcode_line" style="color:#d2d2d2;margin-top:3px;">- - - - - - - - - - - - - - - - - - - - - - - - - - - -</div>
                <div class="div_promptboc_determine">确定</div>
        </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery.min.js"></script>
<script type="text/javascript" src="../script/md5.js"></script>
<script type="text/javascript" src="../script/common.js"></script>

<script type="text/javascript">
    var mdivName, mDivMav, mDivIp,mDetermine;
    var mSessionid
    var phoneSystem
    var mDeviceName
    apiready = function() {
        init();
        obtainNativeData();
        initData();
    };

    //初始化View
    function init() {
        phoneSystem = api.systemType;
        var width = api.winWidth / 2;
        var height = api.winHeight / 2;
        mdivName = document.getElementsByClassName('div_name')[0]
        mDetermine = document.getElementsByClassName('div_determine')[0];
        var divPromptboc = document.getElementsByClassName('div_promptboc')[0];
        var divInput = document.getElementsByClassName('div_qrcode_input')[0];
        mDivMav = document.getElementsByClassName('div_mac')[0];
        mDivIp = document.getElementsByClassName('div_ip')[0];
        divInput.value = ''
        mDetermine.style.marginLeft = width - 65 + 'px';
        divPromptboc.style.marginTop = height - 75 + 'px';

        $('.div_determine').click(function() {
            if (mDeviceName === '未命名') {
                $('body').css('background-color','rgba(0, 0, 0, 0.7)');
                $('.div_promptboc').slideDown("slow");
            } else {
                pcQrselfNet(mDeviceName);
            }

        })

        $('.div_cancel').click(function() {
          api.sendEvent({
              name: 'app_equipment',
              extra: {
                  type: 228,
              }
          });
          api.closeWin({});
        })

        $('.div_promptboc_determine').click(function() {
            var name = $('.div_qrcode_input').val();
            if(name === '未命名' || name === ''){
               api.toast({
                   msg: '该名字不符合规则，请重新填写名字',
                   duration: 2000,
                   location: 'bottom'
               });
            }else {
              $('body').css('background-color', '#fff');
              $('.div_promptboc').slideUp(300, function() {
                    $(this).fadeOut(100);
              });
              pcQrselfNet(name);
            }
        });

        api.setStatusBarStyle({
            style: 'dark'
        });

    }

    //获取原生数据
    function obtainNativeData() {
        var appdat1;
        if ('android' === phoneSystem) {
            appdat1 = api.appParam;
            appdat1 = JSON.parse(appdat1);
        } else if ('ios' === phoneSystem) {
            appdat1 = api.pageParam;
        }
        common.getConsole('获取原生数据', JSON.stringify(appdat1))
        var siteAreaid = appdat1.siteAreaid;
        var pattern = appdat1.pattern;
        var userId = appdat1.userId;
        var userToken = appdat1.userToken;
        var appFWQ = appdat1.appFWQ;
        mSessionid = appdat1.sessionid

        $api.setStorage(common.appKey.siteAreaid, siteAreaid);
        $api.setStorage(common.appKey.pattern, pattern);
        $api.setStorage(common.appKey.userId, userId);
        $api.setStorage(common.appKey.userToken, userToken);
        $api.setStorage(common.appKey.appFWQ, appFWQ);
        $api.setStorage(common.appKey.apMac,appdat1.apMac);
    }

    //获取数据
    function initData() {
        common.getApiShowProgress()
        var fwq = $api.getStorage(common.appKey.appFWQ);
        var parame = {
            userid: $api.getStorage(common.appKey.userId),
            sessionid: mSessionid,
        }
        var parames = common.getJSONParame(common.keyword.pcNetQr_pcQRinfo, JSON.stringify(parame))
        common.getConsole('二维码信息接口', JSON.stringify(parame) + '..fwq...' + $api.getStorage(common.appKey.appFWQ))
        common.ajax($api.getStorage(common.appKey.appFWQ), parames, 8, function(ret, err) {
            common.getApiHideProgress()
            if (ret) {
                common.getConsole('二维码请求结果', JSON.stringify(ret));
                if (ret.code == 1) {
                    mDetermine.style.display = 'block'
                    mDeviceName = ret.deviceName
                    mdivName.innerText = ret.title
                    var conent = ret.conent
                    var dd = conent.indexOf('#')
                    mDivIp.innerText = conent.substring(0, dd)
                    mDivMav.innerText = conent.substring(dd + 1, conent.length)
                } else {
                    common.getShowViewError(ret.code, ret.msg)
                }
            } else {
                common.getConsole('二维码请求失败', JSON.stringify(err));
                common.getShowViewError(err.code, err.msg);
            }
        })
    }

    //授权pc联网
    function pcQrselfNet(deviceName) {
        common.getApiShowProgress()
        var fwq = $api.getStorage(common.appKey.appFWQ);
        var parame = {
            userid: $api.getStorage(common.appKey.userId),
            sessionid: mSessionid,
            planTime: '0',
            deviceName: deviceName
        }
        var parames = common.getJSONParame(common.keyword.pcNetQr_pcQrselfNet, JSON.stringify(parame))
        common.getConsole('授权pc联网接口', JSON.stringify(parame) + '..fwq...' + $api.getStorage(common.appKey.appFWQ))
        common.ajax($api.getStorage(common.appKey.appFWQ), parames, 8, function(ret, err) {
            common.getApiHideProgress()
            if (ret) {
                common.getConsole('授权pc联网请求结果', JSON.stringify(ret));
                if (ret.code == 1) {
                      if(ret.connect == 1){
                        api.toast({
                            msg: '授权成功',
                            duration: 3000,
                            location: 'bottom'
                        });
                        setTimeout(function(){
                           api.sendEvent({
                              name: 'app_equipment',
                              extra: {
                                  type: 228,
                              }
                           });
                           api.closeWin({
                           });

                        },1000);
                      }else {
                          common.getShowViewError(ret.connect, ret.msg)
                      }
                } else {
                    common.getShowViewError(ret.code, ret.msg)
                }
            } else {
                common.getConsole('授权pc联网请求失败', JSON.stringify(err));
                common.getShowViewError(err.code, err.msg);
            }
        })
    }

    function getFrameY() {
        var frameheigh = $api.dom('header').offsetHeight;
        var systemType = api.systemType;
        if (api.systemType === 'ios') {
            var systemVersion = api.systemVersion;
            if (parseFloat(systemVersion) >= 11.0) {
                frameheigh = frameheigh + 20;
            }
            return frameheigh;
        }
        return frameheigh;
    }
</script>

</html>
